package com.guarantee.server.common.utils;

import com.guarantee.server.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.InetAddress;

/**
 * @Description 获取spring的bean对象
 * @Author future
 * @Date 2019-08-18
 * @Version Copyright (c) 2019,安徽阡陌网络科技有限公司 All rights reserved.
 */
public class FileUtils {
    private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
    public static String IP_ADDR;
    public static String PDF_DOWNLOAD_URL_DEV;
    public static String PDF_DOWNLOAD_URL_TEST;
    public static String PDF_DOWNLOAD_URL_PRO;
    static{
        try{
            IP_ADDR = InetAddress.getLocalHost().getHostAddress();
            PDF_DOWNLOAD_URL_DEV = "http://" + IP_ADDR + ":8000/api/service/center/project/pdf?pdfUrl=";
            PDF_DOWNLOAD_URL_TEST = "http://119.3.157.223/api/service/center/project/pdf?pdfUrl=";
            PDF_DOWNLOAD_URL_PRO = "http://121.37.153.177/api/service/center/project/pdf?pdfUrl=";
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 上传
     *
     * @param file
     * @param filePath
     * @param fileName
     * @throws Exception
     */
    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    /**
     * 上传
     *
     * @param file
     * @param filePath
     * @param fileName
     * @throws Exception
     */
    public static void uploadFileUtf8(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    public static byte[] getStreamByFilepath(String filePath) throws Exception{
        InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        return buffer;
    }

    public static void uoloadPdfFile(byte[] file,String filePath) throws Exception {
        File f = new File(filePath);
        f.delete();
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(file);
        fos.write(file);
        fos.flush();
        fos.close();
    }

    public static HttpServletResponse downloadFile(String path,HttpServletResponse response){
        try {
            logger.info("下载保函pdfUrl: " + path);
            if(StringUtils.isEmpty(path)){
                throw new ServiceException("下载地址为空。。。。。");
            }
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            logger.info("下载保函成功: " + path);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

    public static void remove(String path){
        File file = new File(path);
        if (file.delete()) {
            System.out.println("File deleted successfully");
        }
        else {
            System.out.println("Failed to delete the file");
        }
    }
}
